import allure
from page_base.page_base import Base
from page_element.el_A3mall import *
from utils.timer import sleep
import logging



class PageLogin(Base):
    """登录页面"""
    def page_login(self, username, password, casename, expected):
        # 打开A3mall
        self.open_url(url)
        self.maximize_window()
        with allure.step(f"点击请登录"):
            self.click(login_pe.login_window)
        with allure.step(f"输入用户名: {username}"):
            self.send_keys(login_pe.login_username, username)
        with allure.step(f"输入密码: {password}"):
            self.send_keys(login_pe.login_pwd, password)
        # 强制等待输入验证码
        sleep(10)
        with allure.step("点击登录按钮"):
            self.click(login_pe.login_btn)
        # 断言
        with allure.step("验证登录"):
            if '登录成功' in casename:
                dy = self.text(home_pe.home_person_text)
                logging.info(f"预期结果：{expected}，实际结果: {dy}")
                assert expected in dy, f"断言失败(验证失败): 期望包含'{expected}'，实际得到'{dy}'"
                logging.info(f"验证成功,{casename}")
            else:
                dy = self.text(login_pe.login_dy_text)
                logging.info(f"预期结果：{expected}，实际结果: {dy}")
                assert expected == dy, f"断言失败(验证失败): 期望等于'{expected}'，实际得到'{dy}'"
                logging.info(f"验证成功,{casename}")
